package script;

import java.util.ArrayList;

import main.Def;

public class EString 
{
	public class EChar
	{
		public char m_char;
		public boolean m_isTrans;
		
		public EChar(char ch, boolean isTran)
		{
			m_char = ch;
			m_isTrans = isTran;
		}
	}
	
	private ArrayList<EChar> m_list;
	
	public EString(String str)
	{
		m_list = new ArrayList<>();
		boolean isTran = false;
		for(int i = 0; i < str.length(); ++i)
		{
			char ch = str.charAt(i);
			if(isTran)
			{
				m_list.add(new EChar(ch, true));
				isTran = false;
			}
			else
			{
				if(ch == Def.TRANS_CHAR)
				{
					isTran = true;
				}
				else 
				{
					m_list.add(new EChar(ch, false));
				}
			}
		}
	}
	
	public EString(ArrayList<EChar> list)
	{
		m_list = list;
	}
	
	public EChar charAt(int index)
	{
		return m_list.get(index);
	}
	
	public String toString()
	{
		return toString(0, m_list.size());
	}

	public String toString(int start, int end)
	{
		String out = "";
		for(; start < end; ++start)
		{
			EChar ch = m_list.get(start);
			out += ch.m_char;
		}
		return out;
	}
	
	private ArrayList<EChar> subList(int start, int end, ArrayList<EChar> list)
	{
		ArrayList<EChar> out = new ArrayList<>();
		for(; start < end; ++start)
		{
			EChar ch = list.get(start);
			out.add(new EChar(ch.m_char, ch.m_isTrans));
		}
		return out;
	}
	
	public ArrayList<EString> split(char ch)
	{
		return split(ch, 0, m_list.size());
	}
	
	public ArrayList<EString> split(char ch, int start, int end)
	{
		ArrayList<EString> list = new ArrayList<>();
		for(int i = start; i < end; ++i) 
		{
			EChar ech = m_list.get(i);
			if(ech.m_char == ch && !ech.m_isTrans)
			{
				list.add(subString(start, i));
				start = i + 1;
			}
		}
		list.add(subString(start, end));
		return list;
	}
	
	public EString subString(int start)
	{
		return subString(start, m_list.size());
	}
	
	public EString subString(int start, int end)
	{
		if(start >= end)
		{
			return new EString(new ArrayList<EChar>());
		}
		ArrayList<EChar> list = subList(start, end, m_list);
		return new EString(list);
	}
	
	public int find(char ch)
	{
		return find(ch, 0, m_list.size());
	}
	
	public int find(char ch, int start)
	{
		return find(ch, start, m_list.size());
	}
	
	public int find(char ch, int start, int end)
	{
		for(;start < end; ++start)
		{
			EChar ech = m_list.get(start);
			if(ech.m_char == ch && !ech.m_isTrans)
			{
				return start;
			}
		}
		return -1;
	}
}
